package com.controller;

import com.service.StatisticsService;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("index")
@RestController
public class IndexController {


    @Autowired
    private StatisticsService statisticsService;

    /**
     * 获取成绩分布数据(饼图)
     */
    @GetMapping("/scoreDistribution")
    public R getScoreDistribution() {
        try {
            // 返回饼图所需数据格式
            List<Map<String, Object>> data = statisticsService.getStudentScoreDistribution();
            // data格式示例:
            // [
            //   {"name": "优秀", "value": 25},
            //   {"name": "良好", "value": 35},
            //   {"name": "及格", "value": 30},
            //   {"name": "不及格", "value": 10}
            // ]
            return R.ok().put("data", data);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取科目成绩统计(柱状图)
     */
    @GetMapping("/subjectScores")
    public R getSubjectScores() {
        try {
            // 返回柱状图所需数据格式
            List<Map<String, Object>> data = statisticsService.getStudentAvgScores();
            // data格式示例:
            // [
            //   {"name": "张三", "avgScore": 95.5},
            //   {"name": "李四", "avgScore": 88.5},
            //   {"name": "王五", "avgScore": 85.0},
            //   {"name": "赵六", "avgScore": 82.5}
            // ]
            return R.ok().put("data", data);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
